package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.ListNode;
import org.springframework.util.Assert;

/**
 * @ClassName MergeTwoListRecursive
 * @Description MergeTwoListRecursive
 * @Author wanghaiwei
 * @Date 2022/11/22 9:50
 */
public class MergeTwoListIterator {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode resultHead = new ListNode(-999);
        ListNode prev = resultHead;
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                prev.next = list1;
                list1 = list1.next;
            } else {
                prev.next = list2;
                list2 = list2.next;
            }
            prev = prev.next;
        }
        prev.next = list1 == null ? list2 : list1;
        return resultHead.next;
    }

    public static void main(String[] args) {
        ListNode list11 = new ListNode(1);
        ListNode list12 = new ListNode(2);
        ListNode list13 = new ListNode(4);
        ListNode list21 = new ListNode(1);
        ListNode list22 = new ListNode(3);
        ListNode list23 = new ListNode(4);
        list11.next = list12;
        list12.next = list13;
        list21.next = list22;
        list22.next = list23;
        MergeTwoListIterator solution = new MergeTwoListIterator();
        String expected = "1,1,2,3,4,4";
        ListNode resultLink = solution.mergeTwoLists(list11, list21);
        String resultStr = resultLink.toString();
        Assert.isTrue(expected.equals(resultStr), "结果错误");
    }
}
